<script setup lang="ts">
  import {NO_DATA_FOUND_TEXT} from '~/common/constants'
  import {SUCCESS_CODE} from "~/common/constants";

  const nuxtApp = useNuxtApp()

  const userAuthologyList = ref([])
  const editCollectData = ref(null)
  const collectModal = ref(null)
  async function getCollectionList(){
    const {data, msg} = await nuxtApp.$request('/u/collection',{
      method: 'GET',
      server: true,
      key: 'getCollectionList'
    }).catch(e => {
      // nuxtApp.$mutual['notification'].error({
      //   content: e.message,
      //   duration: 5000
      // })
    })
    userAuthologyList.value = data || []
  }
  getCollectionList()
  function deleteById(id:string) {
    nuxtApp.$mutual['dialog'].warning({
      title: '提示',
      content: '确定删除？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const {data, code, msg} = await nuxtApp.$request(`/u/collection/${id}`,{
          method: 'DELETE',
          server: true,
          key: 'deleteCollectionById'
        }).catch(e => {
          // nuxtApp.$mutual['notification'].error({
          //   content: e.message,
          //   duration: 5000
          // })
        })
        if (code === SUCCESS_CODE) {
          nuxtApp.$mutual['notification'].success({
            content: msg,
            duration: 3000
          })
          getCollectionList()
        }
      },
      onNegativeClick: () => {
      }
    })
  }
  function handles(type: string, id: string | number | object) {
    if (type === 'delete') deleteById(id)
    else {
      editCollectData.value = {...id}
      setTimeout(collectModal.value.modalOpen)
    }
  }
</script>
<template>
  <div class="collection-list-box">
    <div class="list-header">
      <div class="list-buttons">
        <span
        href="/user/37841185539176/collections?type=created"
        class="list-button active">
        我创建的 {{userAuthologyList.length}}
      </span> <span href="/user/37841185539176/collections?type=following" class="list-button">
        我订阅的 0
      </span></div>
      <ArticleCollectModal ref="collectModal" @change="getCollectionList" :collect="editCollectData"></ArticleCollectModal>
    </div>
    <div class="collection-list" v-if="userAuthologyList.length">
      <ul class="collection-list">
        <li class="item" v-for="(item,index) in userAuthologyList" :key="item.id">
          <div class="collection-card">
            <a
            :href="`/collection/${item.id}`"
            target="_blank"
            rel="">
            <div class="content-wrap">
              <div class="card-item create-item">
                <div class="title-box"><span class="title">{{item.name}}</span>
                  <svg width="16" height="16" viewBox="0 0 16 16" fill="none"
                       xmlns="http://www.w3.org/2000/svg" class="permission-icon">
                    <path fill-rule="evenodd" clip-rule="evenodd"
                          d="M10.3323 4.73333V6.60034H5.66563V4.73333C5.66563 3.44467 6.71029 2.4 7.99896 2.4C9.28762 2.4 10.3323 3.44467 10.3323 4.73333ZM2.39844 8.60034C2.39844 7.5404 3.22297 6.67304 4.26562 6.60468V4.73333C4.26562 2.67147 5.9371 1 7.99896 1C10.0608 1 11.7323 2.67147 11.7323 4.73333V6.60475C12.7745 6.67361 13.5984 7.54075 13.5984 8.60034V13.0003C13.5984 14.1049 12.703 15.0003 11.5984 15.0003H4.39844C3.29387 15.0003 2.39844 14.1049 2.39844 13.0003L2.39844 8.60034ZM7.99805 12.2009C8.77125 12.2009 9.39805 11.5741 9.39805 10.8009C9.39805 10.0277 8.77125 9.40088 7.99805 9.40088C7.22485 9.40088 6.59805 10.0277 6.59805 10.8009C6.59805 11.5741 7.22485 12.2009 7.99805 12.2009Z"
                          fill="#C2C8D1"></path>
                  </svg>
                </div>
                <div title="tsestsf" class="content-box">
                  {{item.description}}
                </div>
                <div class="footer-box">
                  <div class="footer-left">
                    0篇文章 ·
                    0订阅
                  </div>
                  <div class="footer-right">
                    <div class="button-item" @click.stop.prevent="handles('edit', item)">
                      <svg width="12" height="12" viewBox="0 0 12 12" fill="none"
                           xmlns="http://www.w3.org/2000/svg" class="button-icon">
                        <path fill-rule="evenodd" clip-rule="evenodd"
                              d="M6.987 0.823718L1.31067 6.56524L0.525133 10.1787C0.445929 10.5411 0.55664 10.9188 0.81891 11.1811C1.08118 11.4434 1.45893 11.5541 1.82128 11.4749L5.43477 10.6893L11.1189 4.93838C11.5405 4.51231 11.5387 3.82578 11.115 3.40186L8.53294 0.819005C8.32768 0.613929 8.04914 0.499127 7.75899 0.500011C7.46884 0.500896 7.191 0.617395 6.987 0.823718ZM2.67439 10.2916L1.70503 9.32307L2.19992 7.0505L4.94932 9.7999L2.67439 10.2916Z"
                              fill="#8A919F"></path>
                      </svg>
                      编辑
                    </div>
                    <div class="button-item" @click.stop.prevent="handles('delete', item.id)">
                      <svg width="12" height="12" viewBox="0 0 12 12" fill="none"
                           xmlns="http://www.w3.org/2000/svg" class="button-icon">
                        <path fill-rule="evenodd" clip-rule="evenodd"
                              d="M7.86625 1.62409H10.5475C10.7998 1.62409 11.0043 1.82858 11.0043 2.08084V2.96284C11.0043 3.21509 10.7998 3.41959 10.5475 3.41959H1.45675C1.2045 3.41959 1 3.21509 1 2.96284V2.08084C0.999802 1.95964 1.04786 1.84335 1.13356 1.75765C1.21926 1.67195 1.33555 1.62389 1.45675 1.62409H4.1275V1.25009C4.1275 0.830088 4.471 0.75 4.894 0.75H7.09675C7.51975 0.75 7.86625 0.830088 7.86625 1.25009V1.62409ZM2.41441 4.41952H9.58817C9.7192 4.41957 9.84391 4.4759 9.93058 4.57419C10.0172 4.67248 10.0575 4.80326 10.0412 4.93327L9.30616 10.4998C9.24922 10.9487 8.86773 11.2856 8.41516 11.2865H3.58741C3.13628 11.2837 2.75692 10.9473 2.70016 10.4998L1.96441 4.93327C1.94837 4.80382 1.98835 4.67368 2.07429 4.57556C2.16024 4.47744 2.28398 4.42067 2.41441 4.41952Z"
                              fill="#8A919F"></path>
                      </svg>
                      删除
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </a>
          </div>
        </li>
      </ul>
    </div>
    <NEmpty v-else :description="NO_DATA_FOUND_TEXT"/>
  </div>
</template>
<style scoped lang="scss">
  .collection-list-box {
    background-color: #ffffff;
    .list-buttons {
      display: flex;
      align-items: center;
      .list-button {
        background: #f2f3f5;
        font-size: 14px;
        height: 32px;
        line-height: 32px;
        padding: 0 12px;
        border-radius: 50px;
        &.active {
          font-weight: 500;
          background: #eaf2ff;
          color: #1e80ff;
        }
        &:first-child {
          margin-right: 16px;
        }
      }
    }
    .list-header {
      display: flex;
      justify-content: space-between;
      padding: 12px 20px 4px;
      border-bottom: none;
    }
    .list-create {
      display: flex;
      align-items: center;
      color: #1e80ff;
      font-size: 14px;
      cursor: pointer;
    }
    .add-icon {
      width: 10px;
      height: 10px;
      margin-right: 5px;
    }
    .collection-list {
      min-height: 40vh;
      align-items: flex-start;
      background-color: #fff;
      width: 100%;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .collection-list {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        background: #f4f5f5;
      }
    }
    ul.collection-list .item {
      margin: 0;
      width: 100%;
      box-sizing: border-box;
      background: #fff;
      a {
        text-decoration: none;
        cursor: pointer;
        color: #909090;
      }
      .collection-card{
        .content-wrap {
          margin: 0 20px;
        }
        .card-item {
          position: relative;
          padding: 12px 0;
          box-sizing: border-box;
          .permission-icon {
            margin-left: 4px;
          }
          &:after {
            content: "";
            position: absolute;
            left: 0;
            right: 0;
            border-bottom: 1px solid rgba(228,230,235,.5);
            bottom: 0;
            z-index: 1;
          }
        }
        .title-box {
          line-height: 24px;
          display: flex;
          align-items: center;
          .title {
            color: #252933;
            font-weight: 500;
            font-size: 16px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-all;
            line-height: 24px;
          }
        }
        .content-box {
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          word-break: break-all;
          color: #515767;
          font-size: 14px;
          line-height: 22px;
          margin-top: 8px;
        }
        .footer-box {
          font-size: 13px;
          line-height: 22px;
          margin-top: 8px;
          color: #8a919f;
          display: flex;
          justify-content: space-between;
          .footer-left {
            display: flex;
            align-items: center;
          }
          .footer-right {
            display: none;
            .button-item {
              display: flex;
              align-items: center;
              color: #8a919f;
              font-size: 13px;
              cursor: pointer;
              :last-child {
                margin-left: 24px;
              }
            }
            .button-icon {
              width: 12px;
              height: 12px;
              margin-right: 4px;
            }
          }
        }
      }
      &:hover {
        .footer-right {
          display: flex !important;
        }
      }
    }
  }
</style>
